<template>
  <div class="goods-profile-page">
    <!-- 返回按钮 -->
    <div class="back-section">
      <el-button @click="goBack" type="primary" icon="ArrowLeft">
        返回商品页面
      </el-button>
    </div>

    <!-- 商品详情 -->
    <div class="goods-detail-container" v-if="goods && Object.keys(goods).length > 0">
      <div class="goods-image-section">
        <img :src="goods.image || '@/assets/default-goods.png'" :alt="goods.name" class="goods-image"/>
      </div>

      <div class="goods-info-section">
        <h1 class="goods-title">{{ goods.name }}</h1>
        <div class="goods-price">¥{{ goods.price }}</div>

        <div class="goods-meta">
          <div class="meta-item">
            <span class="meta-label">分类:</span>
            <span class="meta-value">{{ goods.category }}</span>
          </div>
          <div class="meta-item">
            <span class="meta-label">标题:</span>
            <span class="meta-value">{{ goods.title }}</span>
          </div>
        </div>

        <div class="goods-description">
          <h3>商品详情</h3>
          <div class="description-content">
            {{ goods.detail || '暂无详细描述' }}
          </div>
        </div>

        <div class="goods-actions">
          <el-button type="primary" size="large" @click="goToOrder(goods.id)">立即购买</el-button>
        </div>
      </div>
    </div>

    <!-- 加载状态 -->
    <div v-else-if="loading" class="loading">
      加载中...
    </div>

    <!-- 错误状态 -->
    <div v-else class="error">
      未找到商品信息
    </div>
  </div>
</template>

<script>
import {ref, onMounted} from 'vue'
import {useRoute, useRouter} from 'vue-router'
import goodsApi from '@/api/goods_api.js'
import {ArrowLeft} from '@element-plus/icons-vue'

export default {
  name: 'GoodsProfilePage',
  components: {
    ArrowLeft
  },
  setup() {
    const route = useRoute()
    const router = useRouter()
    const goods = ref({})
    const loading = ref(false)

    // 获取商品详情
    const fetchGoodsDetail = async (id) => {
      if (!id) return

      loading.value = true
      try {
        const res = await goodsApi.getGoodsById(id)
        if (res || res.code === 200) {
          goods.value = res || {}
        } else {
          console.error('获取商品详情失败:', res)
        }
      } catch (err) {
        console.error('获取商品详情失败:', err)
      } finally {
        loading.value = false
      }
    }

    // 返回商品页面
    const goBack = () => {
      router.go(-1) // 返回上一页
    }

    // 跳转到订单页面
    const goToOrder = (id) => {
      router.push(`/user/order-profile/${id}`)
    }

    // 页面加载时获取商品详情
    onMounted(() => {
      const goodsId = route.params.id || route.query.id
      fetchGoodsDetail(goodsId)
    })

    return {
      goods,
      loading,
      goBack,
      goToOrder
    }
  }
}
</script>

<style scoped>
.goods-profile-page {
  background-color: #fff;
  padding: 30px;
  max-width: 1400px;
  margin: 0 auto;
}

.back-section {
  margin-bottom: 25px;
}

.goods-detail-container {
  display: flex;
  gap: 40px;
  margin-top: 25px;
}

.goods-image-section {
  flex: 1;
  text-align: center;
}

.goods-image {
  width: 100%;
  height: 600px;
  object-fit: contain;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  background-color: #f8f9fa;
  padding: 20px;
}

.goods-info-section {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.goods-title {
  font-size: 32px;
  color: #333;
  margin-bottom: 20px;
  font-weight: 600;
}

.goods-price {
  font-size: 36px;
  color: #e64340;
  font-weight: bold;
  margin-bottom: 30px;
  padding: 15px 0;
  border-bottom: 1px solid #eee;
}

.goods-meta {
  margin-bottom: 30px;
  padding: 20px;
  background-color: #f8f9fa;
  border-radius: 8px;
}

.meta-item {
  display: flex;
  margin-bottom: 15px;
  align-items: center;
}

.meta-label {
  font-weight: bold;
  width: 80px;
  color: #666;
  font-size: 16px;
}

.meta-value {
  flex: 1;
  color: #333;
  font-size: 16px;
}

.goods-description h3 {
  font-size: 22px;
  margin-bottom: 20px;
  color: #333;
  padding-bottom: 10px;
  border-bottom: 1px solid #eee;
}

.description-content {
  color: #666;
  line-height: 1.8;
  white-space: pre-wrap;
  font-size: 16px;
  padding: 15px;
  background-color: #f8f9fa;
  border-radius: 8px;
}

.goods-actions {
  margin-top: 40px;
  padding-top: 20px;
  border-top: 1px solid #eee;
}

.goods-actions .el-button {
  width: 100%;
  height: 50px;
  font-size: 18px;
  font-weight: bold;
}

.loading, .error {
  text-align: center;
  padding: 80px 20px;
  color: #999;
  font-size: 20px;
}

@media (max-width: 992px) {
  .goods-detail-container {
    flex-direction: column;
  }

  .goods-image {
    height: 500px;
  }

  .goods-title {
    font-size: 28px;
  }

  .goods-price {
    font-size: 32px;
  }

  .goods-profile-page {
    padding: 20px;
  }
}

@media (max-width: 768px) {
  .goods-image {
    height: 400px;
  }

  .goods-title {
    font-size: 24px;
  }

  .goods-price {
    font-size: 28px;
  }

  .meta-label {
    width: 60px;
    font-size: 14px;
  }

  .meta-value {
    font-size: 14px;
  }

  .goods-description h3 {
    font-size: 20px;
  }
}

@media (max-width: 576px) {
  .goods-image {
    height: 300px;
  }

  .goods-title {
    font-size: 22px;
  }

  .goods-price {
    font-size: 24px;
  }

  .goods-actions .el-button {
    height: 45px;
    font-size: 16px;
  }

  .goods-profile-page {
    padding: 15px;
  }
}
</style>
